package com.huatai.datacenter.entity.messagequeuemonitor;


import com.google.common.base.Charsets;
import com.huatai.datacenter.constant.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.PullResult;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.admin.OffsetWrapper;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 一个主题一个消费组下的，一条队列状态信息(队列是最小的粒度)
 *
 * @author lizhong
 */
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "一个主题一个消费者一个分区的消费情况", description = "一个主题一个消费者一个分区的消费情况")
public class QueueStatInfo {

	// RMQ的 TODO 后续改造这个全局变量
	String namesrvAddr = "localhost:9876";

	// 所在的broker名
	@ApiModelProperty("队列所在的broker名")
	private String brokerName;

	// 所在的broker名
	@ApiModelProperty("队列所在的brokerAddr")
	private String brokerAddr;

	// 该主题
	@ApiModelProperty("该主题")
	private String topic;

	// 该消费者
	@ApiModelProperty("该消费者")
	private String group;

	// 队列id
	@ApiModelProperty("队列id")
	private int queueId;

	// 该队列的生产位移
	@ApiModelProperty("该队列的生产位移")
	private long brokerOffset;

	// 该队列的消费位移
	@ApiModelProperty("该队列的消费位移")
	private long consumerOffset;

	// 该队列的消息堆积数
	@ApiModelProperty("该队列的消息堆积数")
	private long lag;

	private String clientInfo;

	// 最后消费时间
	@ApiModelProperty("最后消费时间")
	private long lastTimestamp;

	// 该主题该消费者该队列的堆积消息列表
	@ApiModelProperty("该主题该消费者该队列的堆积消息列表")
	private List<LagRecord> lagRecordList = new ArrayList<>();

	public QueueStatInfo(String topic, String group, int queueId, long brokerOffset, long consumerOffset, long lag) {
		this.topic = topic;
		this.group = group;
		this.queueId = queueId;
		this.brokerOffset = brokerOffset;
		this.consumerOffset = consumerOffset;
		this.lag = lag;
	}

	public QueueStatInfo(String topic, int queueId, long brokerOffset, long consumerOffset) {
		this.topic = topic;
		this.queueId = queueId;
		this.brokerOffset = brokerOffset;
		this.consumerOffset = consumerOffset;
	}
}
